/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package engine165.renderSystem;

import engine165.renderSystem.interfaces.*;
import graphicslib3D.*;
import java.io.*;
import java.util.Scanner;
import java.util.Vector;

/**
 * Implements the camera script
 * @author Matthew
 */
public class CameraScript implements ICameraScriptCollection
{
    private File scriptFile;
    private Vector<ScriptObject> scriptCollection = new Vector<ScriptObject>();
    
    private int numberOfLines = 0;
    private int currentLineNumber = 0;
    
    /**
     * Class for managing the script iterator
     */
    private class ScriptIterator implements ICameraScriptIterator
    {
        private int currentIndex;
        
        /**
         * Constructor for script iterator
         */
        public ScriptIterator()
        {
            currentIndex = 0;
        }

        /**
         * checks to see if another object in the collection is avaliable
         * @return
         */
        public
        boolean hasNext()
        {
            if (scriptCollection.isEmpty())
            {
                return false;
            }
            else if (currentIndex >= scriptCollection.size())
            {
                return false;
            }
            else 
            {
                return true;
            }
        }

        /**
         * returns the next object in the collection
         * @return
         */
        public
        ScriptObject getNext()
        {
            return scriptCollection.elementAt(currentIndex++);
        }
        
        /**
         * Returns the number of scriptObjects
         * @return
         */
        public int getNumber()
        {
            return scriptCollection.size();
        }
    }
    
    /**
     * Contructs the camera script
     * @param fileName File path of script
     * @throws java.io.FileNotFoundException
     */
    public CameraScript(String fileName) throws FileNotFoundException
    {
        scriptFile = new File(fileName);
        parseFile();        
    }
    
    /**
     * Parses the script file
     * @throws java.io.FileNotFoundException
     */
    private void parseFile() throws FileNotFoundException
    {
        Scanner scanner = new Scanner(scriptFile);
        try
        {
            while (scanner.hasNextLine())
            {
                processLine(scanner.nextLine());
            }
        }
        finally
        {
            scanner.close();
        }
    }
    
    /**
     * Process a line of the script file
     * @param line
     */
    private void processLine(String line)
    {
        Scanner scanner = new Scanner(line);
        scanner.useDelimiter(" ");
        
        if (line.startsWith("#"))
        {
            //Do nothing
        }
        else if (scanner.hasNext())
        {
            if (numberOfLines == 0)
            {
                numberOfLines = scanner.nextInt();
                currentLineNumber = 0;
            }
            else if (currentLineNumber < numberOfLines)
            {
                float xPos = scanner.nextFloat();
                float yPos = scanner.nextFloat();
                float zPos = scanner.nextFloat();
                float xRot = scanner.nextFloat();
                float yRot = scanner.nextFloat();
                float zRot = scanner.nextFloat();
                float time = scanner.nextFloat();
                
                scriptCollection.add(new ScriptObject(new Point3D(xPos, yPos, zPos), xRot, yRot, zRot, time));
                
                currentLineNumber++;
            }
        }
        
        scanner.close();
    }

    /**
     * Returns an iterator for the scriptcollection
     * @return
     */
    public
    ICameraScriptIterator getIterator()
    {
        return new ScriptIterator();
    }
}
